﻿using System;
using System.Net;
using System.Windows;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using ListIt.DAL;

namespace ListItTaxi.Web
{
    public class JobManager
    {
        /*#region Private Variable
        private Timer _BackLogTimer;
        private Timer _DormantJobTimer;
        private List<Job> _StagingQueue;
        private PushJobDelegate _PushDelegate;
        private ListItEntities _ListitEntities;
        #endregion

        #region Constructors
        public JobManager(PushJobDelegate pushDelegate, ListItEntities listItEntities)
        {
            _BackLogTimer = new Timer(CheckJobBackLog, "backlog tick", 5000, 30000); // timer starts after 5 seconds and ticks every 30 seconds  
            _DormantJobTimer = new Timer(CheckDormantJobs, "dormant tick", 5000, 60000); // timer starts after 5 seconds and ticks every minute
            _StagingQueue = new List<Job>();
            _PushDelegate = pushDelegate;
            _ListitEntities = listItEntities;
        }
        #endregion

        #region Background Processes
        private void CheckJobBackLog(object state)
        {
        }

        private void CheckDormantJobs(object state)
        {
            
        }*/

        private void MoveActiveJobToStagingQueue(int jobID)
        {
            /****************************************************************************
             * Taxi driver had accepted the job but decided that he is no long interested.  
             * In this situation the job will need to be removed from the active job table 
             * and re-inserted in the job backlog and job processing queue so that a new 
             * taxi can be assign to it.
             * **************************************************************************/
           /* var activeJob = from activeJobs in _ListitEntities.ActiveJobs
                            where activeJobs.ID == jobID
                            select activeJobs;


            foreach (var job in activeJob)
            {
                // create and add new job to history job table
                JobHistory jobHistory = JobHistory.CreateJobHistory(0, job.LicenseNo, job.PickupAddress, job.DestinationAddress, DateTime.Now, job.DeviceID);
                jobHistory.CustomerName = job.CustomerName;
                jobHistory.MobileNo = job.MobileNo;
                jobHistory.TelNo = job.TelNo;
                jobHistory.SpecialInstruction = job.SpecialInstruction;
                jobHistory.TimeCompleted = DateTime.Now;

                _ListitEntities.AddToJobHistories(jobHistory);

                // remove the job from the backlog table
                _ListitEntities.ActiveJobs.DeleteObject(job);
                _ListitEntities.SaveChanges();
            }
        }

        private void MoveActiveJobToHistory(int jobID)
        {
            /****************************************************************************
             * Completed jobs are moved into the history table and a completed time is 
             * added to the record.
             * **************************************************************************/

            var activeJob = from activeJobs in _ListitEntities.ActiveJobs
                          where activeJobs.ID == jobID
                          select activeJobs;


            foreach (var job in activeJob)
            {
                // create and add new job to history job table
                JobHistory jobHistory = JobHistory.CreateJobHistory(0, job.LicenseNo, job.PickupAddress, job.DestinationAddress, DateTime.Now, job.DeviceID);
                jobHistory.CustomerName = job.CustomerName;
                jobHistory.MobileNo = job.MobileNo;
                jobHistory.TelNo = job.TelNo;
                jobHistory.SpecialInstruction = job.SpecialInstruction;
                jobHistory.TimeCompleted = DateTime.Now;

                _ListitEntities.AddToJobHistories(jobHistory);

                // remove the job from the active job table
                _ListitEntities.ActiveJobs.DeleteObject(job);
                _ListitEntities.SaveChanges();
            }
        }

        private void MoveJobBackLogToStaging(int jobID)
        {
            /****************************************************************************
             * All jobs are added to the backlog table.  Once the job becomes due, then
             * it is added to the staging queue.  The job that is currently in the queue
             * will remain in the backlog table with the flag "inQueue" set to true.  This
             * redundancy ensures that job is not lost in case of a power failure.
             * 
             * Once job are added to the staging queue then it is immediately pushed to 
             * the Taxi community, if a suitable taxi is available.
             * **************************************************************************/

            var backLog = from jobBackLog in _ListitEntities.JobBackLogs
                          where jobBackLog.ID == jobID
                          select jobBackLog;

            var jobList = backLog.ToList<JobBackLog>();

           /* foreach (var job in jobList)
            {
                Job newJob = new Job();
                newJob.JobID = jobID;
                newJob.DispatchTime = DateTime.Now;
                _PushDelegate(jobID);
                job.InQueue = true;

                var x = from taxis in _ListitEntities.Taxis
                             where (taxis.Available == true)
                             select taxis;

                var taxiList = x.ToList<Taxi>();
                foreach(var taxi in taxiList) {
                    var distanceBetween = GeoStaicClass.distance(job.PickupLatitude, job.PickupLongituted, taxi.Latitude, taxi.Longituted,'K');
                }






               
                     distanceBetween = job.PickupCoordinates.Latitude, _TaxiCustomer.PickupCoordinates.Longitude,
                            Convert.ToDouble(taxi.Latitude), Convert.ToDouble(taxi.Longituted), 'K');

                    TaxiDriver driver = new TaxiDriver();
                    driver.LicenseNo = taxi.LicenseNo;
                    driver.ETAinMunites = Convert.ToInt32((distanceBetween / 50) * 60);  // 50Km/h

                    listAvailableTaxi.Items.Add(driver);
                }

                // update record status
                _ListitEntities.SaveChanges();
            }*/
        }

        private void MoveStagingJobToActiveJob(int jobID, string licenseNo)
        {
            // create and add new job to active job table
            /*ActiveJob activeJob = ActiveJob.CreateActiveJob(0, licenseNo, job.PickupAddress, job.DestinationAddress, DateTime.Now, job.DeviceID);
            activeJob.CustomerName = job.CustomerName;
            activeJob.MobileNo = job.MobileNo;
            activeJob.TelNo = job.TelNo;
            activeJob.SpecialInstruction = job.SpecialInstruction;*/
        }
        #endregion

        #region Manager Controls
        public void Start()
        {
        }

        public void Pause()
        {
        }

        public void SetJobStatus(int jobID)
        {
        }

       
        #endregion
    }
}